package com.xywl.escort.view.evaluate

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.OrderEvaluateAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.CommentAddReq
import com.xywl.escort.bean.PurchaseOrderDetailBean
import com.xywl.escort.utils.NetworkUtil
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_order_evaluate.*
import kotlinx.android.synthetic.main.title_bar.*
import timber.log.Timber
import javax.inject.Inject

@Route(path = "/xy/order/evaluate")
class OrderEvaluateActivity : BaseActivity(), OrderEvaluateContract.View {
    private lateinit var mAdapter: OrderEvaluateAdapter

    @Autowired
    @JvmField
    var orderId: String = ""

    private val orderItems = java.util.ArrayList<PurchaseOrderDetailBean.OrderItemsBean>()

    @Inject
    lateinit var mPresenter: OrderEvaluateContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order_evaluate)
        ARouter.getInstance().inject(this)
        initView()
        initData()
        addListener()
    }

    private fun initView() {
        showBackButton()
        setTitleText("评价")
        mAdapter = OrderEvaluateAdapter(R.layout.item_order_evaluate, orderItems)
        recycler_order_evaluate.layoutManager = LinearLayoutManager(this)
        recycler_order_evaluate.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        mAdapter.emptyView = emptyView
    }

    private fun initData() {
        mPresenter.takeView(this)
        mPresenter.loadOrderDetail(orderId)
    }

    private fun addListener() {
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.text_submit -> {
                    submit(position)
                }
            }
        }

        tv_title_right.setOnClickListener {
            addComments(orderItems)
        }
    }

    private fun submit(position: Int) {
        if (!NetworkUtil.isAvailable(this)) {
            ToastUtil.showTip(this, R.string.network_error)
            return
        }
        val bean = orderItems[position]
        if (!TextUtils.isEmpty(bean.commentContent)) {
            addComment(bean)
        } else {
            ToastUtil.showTip(this, "评论内容不能为空")
        }
    }

    private fun addComment(bean: PurchaseOrderDetailBean.OrderItemsBean) {
        val req = CommentAddReq()
        val comments = ArrayList<CommentAddReq.CommentsBean>()
        val commentsBean = CommentAddReq.CommentsBean()
        commentsBean.orderId = bean.orderId
        commentsBean.skuId = bean.skuId
        commentsBean.spuId = bean.spuId
        commentsBean.content = bean.commentContent
        comments.add(commentsBean)
        req.comments = comments
        mPresenter.addComments(req)
    }

    private fun addComments(list: List<PurchaseOrderDetailBean.OrderItemsBean>) {
        if (!NetworkUtil.isAvailable(this)) {
            ToastUtil.showTip(this, R.string.network_error)
            return
        }
        val req = CommentAddReq()
        val comments = ArrayList<CommentAddReq.CommentsBean>()

        for (bean in list) {
            if (!TextUtils.isEmpty(bean.commentContent)) {
                val commentsBean = CommentAddReq.CommentsBean()
                commentsBean.orderId = bean.orderId
                commentsBean.skuId = bean.skuId
                commentsBean.spuId = bean.spuId
                commentsBean.content = bean.commentContent
                comments.add(commentsBean)
            }
        }
        req.comments = comments
        if (comments.size > 0) {
            mPresenter.addComments(req)
        } else {
            ToastUtil.showTip(this, "评论内容不能为空")
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun loadOrderDetailSuccess(dataBean: PurchaseOrderDetailBean?) {
        progress_bar.visibility = View.GONE
        recycler_order_evaluate.visibility = View.VISIBLE
        orderItems.clear()
        orderItems.addAll(dataBean!!.orderItems)
        mAdapter.setNewData(orderItems)
        mAdapter.loadMoreComplete()
        var b = false
        for (item in orderItems) {
            if (TextUtils.isEmpty(item.commentId)) {
                b = true
            }
        }
        if (b) {
            tv_title_right.visibility = View.VISIBLE
            tv_title_right.text = "批量发布"
        } else {
            tv_title_right.visibility = View.GONE
        }
    }

    override fun loadOrderDetailFailure() {
        progress_bar.visibility = View.GONE
        recycler_order_evaluate.visibility = View.VISIBLE
        Timber.e("loadOrderDetailFailure")
    }

    override fun addCommentsFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }

    override fun addCommentsSuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "评价成功")
    }
}
